package com.zmn.plat.dubbo.impl.channel.workorder.picture;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.ChannelWorkOrderPictureTypeEnum;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.common.dto.channel.workorder.picture.ChannelWorkOrderPictureDIO;
import com.zmn.plat.common.dto.channel.workorder.picture.ChannelWorkOrderPictureDRO;
import com.zmn.plat.common.dto.channel.workorder.picture.ChannelWorkOrderPictureStandardDRO;
import com.zmn.plat.common.util.IntegerStringToListUtil;
import com.zmn.plat.dubbo.interfaces.channel.workorder.picture.ChannelWorkOrderPictureListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.ChannelDict;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPicture;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPictureQuery;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPictureStandard;
import com.zmn.plat.services.interfaces.channel.ChannelService;
import com.zmn.plat.services.interfaces.channel.workorder.picture.ChannelWorkOrderPictureService;
import com.zmn.plat.services.interfaces.channel.workorder.picture.ChannelWorkOrderPictureStandardService;
import com.zmn.plat.utils.bean.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.cglib.beans.BeanCopier;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author linfeng
 * @description 渠道-工单照片
 * @modifier
 * @since 2020/09/17 17:14
 **/
@Slf4j
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ChannelWorkOrderPictureListRemoteServiceImpl implements ChannelWorkOrderPictureListRemoteService {
    private static final String TAG = "工单照片 dubbo 接口";
    @Resource
    private ChannelWorkOrderPictureService channelWorkOrderPictureService;
    @Resource
    private ChannelWorkOrderPictureStandardService channelWorkOrderPictureStandardService;

    @Resource
    private ChannelService channelService;

    /**
     * 描述: 根据主键查询工单照片
     *
     * @param id
     * @return ChannelWorkOrderPictureDRO
     * @auther: linfeng
     * @since: 2020/09/17 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<ChannelWorkOrderPictureDRO> getById(Integer id) {
        log.info("ChannelWorkOrderPictureListRemoteServiceImpl.getById参数id:{}", id);

        ChannelWorkOrderPicture channelWorkOrderPicture = channelWorkOrderPictureService.findByKey(id);
        if (Objects.isNull(channelWorkOrderPicture)) {
            log.error("{}参数:{}对应的对象不存在", TAG, "id");
            return ResponseDTO.fail(String.format("[%s]对应的对象不存在", id));
        }

        ChannelWorkOrderPictureDRO dro = new ChannelWorkOrderPictureDRO();
        BeanCopyUtils.copyProperties(channelWorkOrderPicture, dro);
        dro.setServCategId(IntegerStringToListUtil.tranStringToList(channelWorkOrderPicture.getServCategId()));
        dro.setPictureTypeName(ChannelWorkOrderPictureTypeEnum.getName(dro.getPictureType()));
        dro.setBizTypeName(ProductDict.getBizTypeCNENName(dro.getBizType()));
        return ResponseDTO.success(dro);
    }

    /**
     * 描述: 根据条件查询工单照片
     *
     * @param channelWorkOrderPictureDIO
     * @return ChannelWorkOrderPictureDRO
     * @auther: linfeng
     * @since: 2020/09/17 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<ChannelWorkOrderPictureDRO>> listByQuery(ChannelWorkOrderPictureDIO channelWorkOrderPictureDIO) {
        log.info("ChannelWorkOrderPictureListRemoteServiceImpl.listByQuery参数channelWorkOrderPictureDIO：{}", channelWorkOrderPictureDIO);

        ChannelWorkOrderPictureQuery query = new ChannelWorkOrderPictureQuery();
        BeanCopyUtils.copyProperties(channelWorkOrderPictureDIO, query);
        query.setServCategId(null==channelWorkOrderPictureDIO.getServCategId()?null:channelWorkOrderPictureDIO.getServCategId().toString());
        List<ChannelWorkOrderPicture> channelWorkOrderPictures = Optional.ofNullable(channelWorkOrderPictureService.listByQuery(query)).orElse(Collections.emptyList());

        if (channelWorkOrderPictures.isEmpty()) {
            Channel channel = channelService.findByKey(channelWorkOrderPictureDIO.getChannelId());
            if (Objects.isNull(channel)) {
                return ResponseDTO.fail("渠道不存在");
            }
            if (ChannelDict.RANK_ONE == channel.getRank() || null == channel.getParentId()) {
                return ResponseDTO.fail("没有相关渠道照片配置信息");
            }
            query.setChannelId(channel.getParentId());
            channelWorkOrderPictures = Optional.ofNullable(channelWorkOrderPictureService.listByQuery(query)).orElse(Collections.emptyList());
            if (channelWorkOrderPictures.isEmpty()) {
                return ResponseDTO.fail("没有相关渠道照片配置信息");
            }
        }
        BeanCopier copier = BeanCopier.create(ChannelWorkOrderPicture.class, ChannelWorkOrderPictureDRO.class, false);
        List<ChannelWorkOrderPictureDRO> result = channelWorkOrderPictures.stream().map(channelWorkOrderPicture -> {
            ChannelWorkOrderPictureDRO dro = new ChannelWorkOrderPictureDRO();
            copier.copy(channelWorkOrderPicture, dro, null);
            dro.setBizTypeName(ProductDict.getBizTypeCNENName(dro.getBizType()));
            dro.setServCategId(IntegerStringToListUtil.tranStringToList(channelWorkOrderPicture.getServCategId()));
            dro.setPictureTypeName(ChannelWorkOrderPictureTypeEnum.getName(dro.getPictureType()));
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(result);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<ChannelWorkOrderPictureStandardDRO>> listStandardByQuery(@NotNull(message = "入参不能为空") @Valid ChannelWorkOrderPictureDIO channelWorkOrderPictureDIO) {

        log.info("ChannelWorkOrderPictureListRemoteServiceImpl.listStandardByQuery参数channelWorkOrderPictureDIO：{}", channelWorkOrderPictureDIO);

        ChannelWorkOrderPictureQuery query = new ChannelWorkOrderPictureQuery();
        BeanCopyUtils.copyProperties(channelWorkOrderPictureDIO, query);
        query.setServCategId(null==channelWorkOrderPictureDIO.getServCategId()?null:channelWorkOrderPictureDIO.getServCategId().toString());
        List<ChannelWorkOrderPicture> channelWorkOrderPictures = Optional.ofNullable(channelWorkOrderPictureService.listByQuery(query)).orElse(Collections.emptyList());

        if (channelWorkOrderPictures.isEmpty()) {
            Channel channel = channelService.findByKey(channelWorkOrderPictureDIO.getChannelId());
            if (Objects.isNull(channel)) {
                return ResponseDTO.fail("渠道不存在");
            }
            if (ChannelDict.RANK_ONE == channel.getRank() || null == channel.getParentId()) {
                return ResponseDTO.fail("没有相关渠道照片配置信息");
            }
            query.setChannelId(channel.getParentId());
            channelWorkOrderPictures = Optional.ofNullable(channelWorkOrderPictureService.listByQuery(query)).orElse(Collections.emptyList());
            if (channelWorkOrderPictures.isEmpty()) {
                return ResponseDTO.fail("没有相关渠道照片配置信息");
            }
        }

        List<Integer> relatedIds = channelWorkOrderPictures.stream().map(ChannelWorkOrderPicture::getRelateId).collect(Collectors.toList());
        List<ChannelWorkOrderPictureStandard> standardList = channelWorkOrderPictureStandardService.listStandardByRelateIds(relatedIds);

        return ResponseDTO.success(BeanMapper.mapList(standardList, ChannelWorkOrderPictureStandardDRO.class));

    }
}
